package com.ronsai.umtrack.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;

import com.ronsai.umtrack.R;
import com.ronsai.umtrack.base.BaseActivity;
import com.ronsai.umtrack.base.BasePresenter;
import com.ronsai.umtrack.base.BaseView;
import com.ronsai.umtrack.fragment.HomeFragment;
import com.ronsai.umtrack.fragment.MineFragment;
import com.ronsai.umtrack.fragment.NewsFragment;
import com.ronsai.umtrack.fragment.TopicFragment;
import com.ronsai.umtrack.util.StatusBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.nav_home_iv)
    ImageView navHomeIv;
    @BindView(R.id.nav_mood_iv)
    ImageView navMoodIv;
    @BindView(R.id.nav_news_iv)
    ImageView navNewsIv;
    @BindView(R.id.nav_mine_iv)
    ImageView navMineIv;

    private List<View> navViewList = new ArrayList<>();
    private static final String FRAGMENT_TAG_HOME = "HomeFragment";
    private static final String FRAGMENT_TAG_TOPIC = "TopicFragment";
    private static final String FRAGMENT_TAG_NEWS = "NewsFragment";
    private static final String FRAGMENT_TAG_MINE = "MineFragment";
    private HomeFragment mHomeFragment;
    private TopicFragment mTopicFragment;
    private NewsFragment mNewsFragment;
    private MineFragment mMineFragment;
    private Fragment mFragment;
    private int mNavSelection;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected BaseView createView() {
        return null;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        StatusBarUtil.setTranslucentForImageView(this, null);
        StatusBarUtil.setStatusBarMode(this, true);

        initFragment(savedInstanceState);
        initNavView();
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("navSelection", mNavSelection);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //moveTaskToBack(false);

            Intent home = new Intent(Intent.ACTION_MAIN);
            home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            home.addCategory(Intent.CATEGORY_HOME);
            startActivity(home);

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initNavView() {
        navViewList.clear();
        navViewList.add(navHomeIv);
        navViewList.add(navMoodIv);
        navViewList.add(navNewsIv);
        navViewList.add(navMineIv);
        setNavSelection(mNavSelection);
    }

    public void setNavSelection(int position) {
        mNavSelection = position;
        for (int i = 0; i < navViewList.size(); i++) {
            View view = navViewList.get(i);
            if (i == position) {
                view.setSelected(true);
            } else {
                view.setSelected(false);
            }
        }
    }

    private void initFragment(Bundle savedInstanceState) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (savedInstanceState == null) {
            mHomeFragment = new HomeFragment();
            mNewsFragment = new NewsFragment();
            mFragment = mHomeFragment;
            fragmentTransaction.add(R.id.content_frame, mHomeFragment, FRAGMENT_TAG_HOME);
            fragmentTransaction.add(R.id.content_frame, mNewsFragment, FRAGMENT_TAG_NEWS);
            fragmentTransaction.hide(mNewsFragment);
        } else {
            mHomeFragment = (HomeFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_HOME);
            mTopicFragment = (TopicFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_TOPIC);
            mNewsFragment = (NewsFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_NEWS);
            mMineFragment = (MineFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_MINE);
            if (mHomeFragment != null) fragmentTransaction.hide(mHomeFragment);
            if (mTopicFragment != null) fragmentTransaction.hide(mTopicFragment);
            if (mNewsFragment != null) fragmentTransaction.hide(mNewsFragment);
            if (mMineFragment != null) fragmentTransaction.hide(mMineFragment);
            mNavSelection = savedInstanceState.getInt("navSelection");
            switch (mNavSelection) {
                case 1:
                    if (mTopicFragment == null) {
                        mTopicFragment = new TopicFragment();
                        fragmentTransaction.remove(mTopicFragment);
                        fragmentTransaction.add(R.id.content_frame, mTopicFragment, FRAGMENT_TAG_TOPIC);
                    } else {
                        fragmentTransaction.show(mTopicFragment);
                    }
                    mFragment = mTopicFragment;
                    break;
                case 2:
                    if (mNewsFragment == null) {
                        mNewsFragment = new NewsFragment();
                        fragmentTransaction.remove(mNewsFragment);
                        fragmentTransaction.add(R.id.content_frame, mNewsFragment, FRAGMENT_TAG_NEWS);
                    } else {
                        fragmentTransaction.show(mNewsFragment);
                    }
                    mFragment = mNewsFragment;
                    break;
                case 3:
                    if (mMineFragment == null) {
                        mMineFragment = new MineFragment();
                        fragmentTransaction.remove(mMineFragment);
                        fragmentTransaction.add(R.id.content_frame, mMineFragment, FRAGMENT_TAG_MINE);
                    } else {
                        fragmentTransaction.show(mMineFragment);
                    }
                    mFragment = mMineFragment;
                    break;
                case 0:
                default:
                    if (mHomeFragment == null) {
                        mHomeFragment = new HomeFragment();
                        fragmentTransaction.remove(mHomeFragment);
                        fragmentTransaction.add(R.id.content_frame, mHomeFragment, FRAGMENT_TAG_HOME);
                    } else {
                        fragmentTransaction.show(mHomeFragment);
                    }
                    mFragment = mHomeFragment;
                    break;
            }
        }
        fragmentTransaction.commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();
    }

    /**
     * Fragment切换
     */
    private void switchFragment(Fragment toFragment, String tag) {
        FragmentTransaction fg = getSupportFragmentManager().beginTransaction();
        if (!toFragment.isAdded() && getSupportFragmentManager().findFragmentByTag(tag) == null) {
            if (mFragment != null) {
                fg.hide(mFragment);
            }
            fg.add(R.id.content_frame, toFragment, tag);
        } else {
            if (mFragment != null) {
                fg.hide(mFragment);
            }
            fg.show(toFragment);
        }
        fg.commitAllowingStateLoss();
        getSupportFragmentManager().executePendingTransactions();
        mFragment = toFragment;
    }

    @OnClick({R.id.nav_home, R.id.nav_mood, R.id.nav_news, R.id.nav_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.nav_home:
                if (mHomeFragment == null) {
                    mHomeFragment = (HomeFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_HOME);
                    if (mHomeFragment == null) mHomeFragment = new HomeFragment();
                }
                switchFragment(mHomeFragment, FRAGMENT_TAG_HOME);
                setNavSelection(0);
                break;
            case R.id.nav_mood:
                if (mTopicFragment == null) {
                    mTopicFragment = (TopicFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_TOPIC);
                    if (mTopicFragment == null) mTopicFragment = new TopicFragment();
                }
                switchFragment(mTopicFragment, FRAGMENT_TAG_TOPIC);
                setNavSelection(1);
                break;
            case R.id.nav_news:
                if (mNewsFragment == null) {
                    mNewsFragment = (NewsFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_NEWS);
                    if (mNewsFragment == null) mNewsFragment = new NewsFragment();
                }
                switchFragment(mNewsFragment, FRAGMENT_TAG_NEWS);
                setNavSelection(2);
                break;
            case R.id.nav_mine:
                if (mMineFragment == null) {
                    mMineFragment = (MineFragment) getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG_MINE);
                    if (mMineFragment == null) mMineFragment = new MineFragment();
                }
                switchFragment(mMineFragment, FRAGMENT_TAG_MINE);
                setNavSelection(3);
                break;
        }
    }
}
